<?php  require_once(BASEPATH.'application/controllers/validaciones.php');
//vende
class fsiigo extends validaciones {


	function fsiigo(){
		parent::Controller(); 
		$this->load->helper('url');
		$this->load->helper('text');
		$this->load->library("rapyd");
		$this->load->helper('download');
		$this->load->plugin('numletra'); 
	}
	
	function boleta ($numero){																														
																																																																																						
		$mSQL_1  = $this->db->query("SELECT IF(moneda='01','NUEVOS SOLES','DOLARES AMERICANOS')as  tmoneda, 
		nguia,IF(referen='E','CONTADO','CREDITO') AS fpago,a.formap,serie,
		if(moneda='01','Dolares Americanos','Nuevos Soles') as moneda,
		tipo_doc,fecha as fecha,numero,cod_cli,condi1,condi2,condi3,condi4,b.rifci      ,b.dire11 direc, b.dire12 dire1,b.nombre,iva,
		totalg,totals,arancel,paiso,puerto,termino,vd FROM sfac  as a 
		JOIN scli AS b ON a.cod_cli=b.cliente WHERE a.id='$numero'");                                                                     			                                                                                                                                                     
		$row = $mSQL_1->row();                                                                                                                                   
	
		$cod_cli=$row->cod_cli;  
		$vd=$row->vd;
		$monto=$row->totalg;
		
		$letra =numletra($monto);        
		
		$mSQL_2 = $this->db->query("SELECT  INSERT(b.csiigo, 1, 2, '') as csiigo,
		a.codigo, b.descrip AS desca,a.cantidad,a.precio AS preca,
		total AS importe,b.clave as clave,b.modelo,a.unidad,b.marca 
		FROM  sitems as a 
		JOIN sinv as b ON a.codigo=b.codigo WHERE factura='$numero' ORDER BY csiigo");
		$detalle =$mSQL_2->result();
		
		$cant =$mSQL_2->num_rows();
		$cont = (13-$cant);
		
		$line=$error='';
		if ($mSQL_1->num_rows() > 0){
		
			$line.="\r\n";
			$line.="\r\n";	
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
						
			$line.="\t\t\t\t".str_pad($row->nombre,73," ");
			$line.=str_pad('',12,' ')."\r\n"; 
		
			$line.="\t\t\t\t".str_pad($row->direc,73," ");
			$line.=str_pad(dbdate_to_human($row->fecha),12,' ')."\r\n"; 

			$line.="\t\t\t\t".str_pad($row->dire1,80," ")."\r\n";
			
			$line.="\t\t\t\t".str_pad('',73," ");
			$line.=str_pad($row->nguia,12,' ')."\r\n"; 
			
		
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
		
			foreach($mSQL_2->result_array() as $row1){
				
							$line.="\t"."   ".str_pad($row1['csiigo'],11,' ');  
							$line.="  ".str_pad(strtoupper($row1['desca']),44,' ');
							$line.="  ".str_pad($row1['cantidad'],8,' ',STR_PAD_LEFT);   
							$line.="  ".str_pad(number_format($row1['preca'],2),12,' ',STR_PAD_LEFT); 
							$line.="  ".str_pad(number_format($row1['importe'],2),11,' ',STR_PAD_LEFT);    
							$line.="  "."\r\n";
			
			}
		
			
			for ($i = 1; $i < $cont; $i++) {
				$line.="\r\n";
			}
			
			$line.="\r\n";
			$line.=" ".str_pad(number_format($row->totalg,2),101,' ',STR_PAD_LEFT)."\r\n"; 
											
		
		}else{
			$line="\r\n";
		}
		
		$nombre='Boleta de Venta'.'.txt';
		force_download($nombre,$line);                                                                                               	
		
	}
	function factura ($numero){																														
																																																																																						
	$mSQL_1  = $this->db->query("SELECT IF(moneda='01','NUEVOS SOLES','DOLARES AMERICANOS')as  tmoneda, 
		nguia,IF(referen='E','CONTADO','CREDITO') AS fpago,a.formap,serie,
		if(moneda='01','Dolares Americanos','Nuevos Soles') as moneda,
		tipo_doc,fecha as fecha,numero,cod_cli,condi1,condi2,condi3,condi4,b.rifci,b.dire11 direc, b.dire12 dire1,b.nombre,iva,
		totalg,totals,arancel,paiso,puerto,termino,vd FROM sfac  as a 
		JOIN scli AS b ON a.cod_cli=b.cliente WHERE a.id='$numero'");                                                                     			                                                                                                                                                     
		$row = $mSQL_1->row();                                                                                                                                   
	
		$cod_cli=$row->cod_cli;  
		$vd=$row->vd;
		$monto=$row->totalg;
		
		$letra =numletra($monto);        
		
		$mSQL_2 = $this->db->query("SELECT  INSERT(b.csiigo, 1, 2, '') as csiigo,
		a.codigo, b.descrip AS desca,a.cantidad,a.precio AS preca,
		total AS importe,b.clave as clave,b.modelo,a.unidad,b.marca 
		FROM  sitems as a 
		JOIN sinv as b ON a.codigo=b.codigo WHERE factura='$numero' ORDER BY csiigo");
		$detalle =$mSQL_2->result();
		
		$cant =$mSQL_2->num_rows();
		$cont = (29-$cant);
		
		$mSQL_4 = $this->db->query("SELECT  nombre, telefono, mail FROM vend WHERE vendedor='$vd'");
		$row2 = $mSQL_4->row();
		
		$line=$error='';
		if ($mSQL_1->num_rows() > 0){
		
			$line.="\r\n";
			$line.="\r\n";	
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n"; 

			$line.="\t\t\t".str_pad($row->nombre,74," ");
			$line.=str_pad(dbdate_to_human($row->fecha),12,' ')."\r\n"; 
					
			$line.="\t\t\t".str_pad($row->direc,57," ");
			$line.=str_pad($row->fpago,17,' ')."\r\n";  
			
			$line.="\t\t\t".str_pad($row->dire1,57," ");
			$line.=str_pad($row2->nombre,17,' ')."\r\n";
						
			$line.="\t\t\t".str_pad($row->rifci,74," ");
			$line.=str_pad($row->nguia,12,' ')."\r\n"; 
		
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
		
			foreach($mSQL_2->result_array() as $row1){
				
							$line.="  ".str_pad($row1['csiigo'],8,' '); 
							$line.=str_pad($row1['cantidad'],7,' ',STR_PAD_LEFT);   
							$line.="  ".str_pad(strtoupper($row1['desca']),43,' ');  
							$line.="  ".str_pad(number_format($row1['preca'],2),13,' ',STR_PAD_LEFT); 
							$line.="  ".str_pad(number_format($row1['importe'],2),15,' ',STR_PAD_LEFT);    
							$line.="  "."\r\n";
			
			}
		
			
			for ($i = 1; $i < $cont; $i++) {
				$line.="\r\n";
			}
			
			$line.="\r\n";
			$line.="\r\n";
			$line.="\t\t\t\t\t"."  "."SIRVASE ABONAR EN LA SIGUIENTE CUENTA CORRIENTE"."\r\n";
			$line.="\t\t\t\t\t"."  "."DOLARES: BCP:1941901960126  -  BANBIF: 7000220708"."\r\n";
			$line.="\t\t\t\t\t"."  "."SOLES:   BCP:1941937252001  -  BANBIF: 7000220686"."\r\n";	
			$line.="\t\t".strtoupper($letra).' '.$row->tmoneda."\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\t\t\t".str_pad(number_format($row->totals,2),21,' ')." "; 
			$line.=" ".str_pad('0.00',17,' ')." "; 
			$line.=" ".str_pad(number_format($row->totals,2),21,' ')." ";	 
			$line.=" ".str_pad(number_format($row->iva,2),11,' ')." "; 
			$line.=" ".str_pad(number_format($row->totalg,2),10,' ')."\r\n"; 
											
		
		}else{
			$line="\r\n";
		}
		
		$nombre='Factura de Venta'.'.txt';
		force_download($nombre,$line);                                                                                               	
		
	}
	function notasd($numero){
		
		$mSQL_1  = $this->db->query("SELECT IF(moneda='01','NUEVOS SOLES','DOLARES AMERICANOS')as  tmoneda,compro comp,fecha, vd,  cod_cli,  a.rifci,  a.nombre,  direc,  condi1,  
		totals,  iva,  totalg,  cambio,  CONCAT(serief,'-',factura)serief,  factura,  fechaf,  
		b.rifci,b.dire11 direc, b.dire12 dire1 
		FROM notasdc  as a 
		JOIN scli AS b ON a.cod_cli=b.cliente WHERE a.numero='$numero'");                                                                     			                                                                                                                                                     
		$row = $mSQL_1->row();                                                                                                                                   
	
		$cod_cli=$row->cod_cli; 
         
        $monto=$row->totalg;  
        $letra =numletra($monto);      
		
		$mSQL_2 = $this->db->query("SELECT a.cuenta, a.descrip AS descrip,a.total
		FROM itnotasdc as a 
		WHERE numero='$numero' ORDER BY a.cuenta"); 

		$detalle =$mSQL_2->result();
		
		$cant =$mSQL_2->num_rows();
		$cont = (10-$cant);
		
		
		$line=$error='';
		if ($mSQL_1->num_rows() > 0){
		
			$line.="\r\n";
			$line.="\r\n";	
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";

			$line.="\t\t\t\t"." ".str_pad($row->nombre,71," ");
			$line.=str_pad(dbdate_to_human($row->fecha),15,' ')."\r\n"; 
		
			$line.="\t\t\t\t"." ".str_pad($row->direc,71," ")."\r\n"; 
			
			$line.="\t\t\t\t"." ".str_pad($row->dire1,71," ")."\r\n";
						
			$line.="\t\t\t\t"." ".str_pad($row->rifci,71," ");
			$line.=str_pad($row->comp,5,' ')."\r\n"; 

			$line.="\t\t\t\t"." ".str_pad('',71," ");
			$line.=str_pad($row->serief,15,' ')."\r\n";
			
			$line.="\t\t\t\t"." ".str_pad($row->condi1,71," ");
			$line.=str_pad($row->fecha,10,' ')."\r\n";
		
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
		
			foreach($mSQL_2->result_array() as $row1){
				
							$line.="\t"."   ".str_pad('',11,' '); 
							$line.="  ".str_pad($row1['descrip'],44,' ');   
							$line.="  ".str_pad(strtoupper(' '),8,' ');  
							$line.="  ".str_pad(number_format($row1['total'],2),12,' ',STR_PAD_LEFT); 
							$line.="  ".str_pad(number_format($row1['total'],2),11,' ',STR_PAD_LEFT);    
							$line.="  "."\r\n";
			
			}
		
			
			for ($i = 1; $i < $cont; $i++) {
				$line.="\r\n";
			}
			
			$line.="\t\t\t\t"."SON: ".strtoupper($letra).' '.$row->tmoneda."\r\n";
            $line.=" ".str_pad(number_format($row->totals,2),101,' ', STR_PAD_LEFT)."\r\n";	
			$line.="\r\n";
			$line.=" ".str_pad(number_format($row->iva,2),101,' ', STR_PAD_LEFT)."\r\n"; 
			$line.=" ".str_pad(number_format($row->totalg,2),101,' ', STR_PAD_LEFT)."\r\n"; 							
		
		}else{
			$line="\r\n";
		}
		
		$nombre='Nota de Debito'.'.txt';
		force_download($nombre,$line);
	} 
	function notac ($numero){																														
																																																																																						
		$mSQL_1  = $this->db->query("SELECT IF(moneda='01','NUEVOS SOLES','DOLARES AMERICANOS')as  tmoneda,'F' comp,fecha, vd,  cod_cli,  a.rifci,  a.nombre,  direc,  condi1,  
		totals,  iva,  totalg,  cambio,  CONCAT(serief,'-',factura)serief,  factura,  fechaf,  
		b.rifci,b.dire11 direc, b.dire12 dire1 
		FROM notascc  as a 
		JOIN scli AS b ON a.cod_cli=b.cliente WHERE a.numero='$numero'");                                                                     			                                                                                                                                                     
		$row = $mSQL_1->row();                                                                                                                                   
	
		$cod_cli=$row->cod_cli; 
        
        $monto=$row->totalg;  
        $letra =numletra($monto);       
		
		$mSQL_2 = $this->db->query("SELECT  IF(c.almacen='0000',a.codigo,INSERT(b.csiigo, 1, 2, '')) as csiigo,
		a.codigo, a.desca AS descrip,a.cana,a.preca,
		importe AS importe 
		FROM itnotascc as a
		JOIN notascc as c on a.id=c.id 
		LEFT JOIN sinv as b ON a.codigo=b.codigo WHERE a.numero='$numero' ORDER BY csiigo"); 

		$detalle =$mSQL_2->result();
		
		$cant =$mSQL_2->num_rows();
		$cont = (11-$cant);
		
		
		$line=$error='';
		if ($mSQL_1->num_rows() > 0){
		
			$line.="\r\n";
			$line.="\r\n";	
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";

			$line.="\t\t\t\t".str_pad($row->nombre,72," ");
			$line.=str_pad(dbdate_to_human($row->fecha),15,' ')."\r\n"; 
		
			$line.="\t\t\t\t".str_pad($row->direc,72," ")."\r\n"; 
			
			$line.="\t\t\t\t".str_pad($row->dire1,72," ")."\r\n";
						
			$line.="\t\t\t\t".str_pad($row->rifci,72," ");
			$line.=str_pad($row->comp,5,' ')."\r\n"; 

			$line.="\t\t\t\t".str_pad('',72," ");
			$line.=str_pad($row->serief,15,' ')."\r\n";
			
			$line.="\t\t\t\t".str_pad($row->condi1,72," ");
			$line.=str_pad($row->fechaf,10,' ')."\r\n";

			$line.="\r\n"; 	
			$line.="\r\n";
			$line.="\r\n";
		
			foreach($mSQL_2->result_array() as $row1){
				
							$line.="\t"."   ".str_pad($row1['csiigo'],11,' '); 
							$line.="  ".str_pad($row1['descrip'],44,' ');   
							$line.="  ".str_pad(strtoupper($row1['cana']),8,' ',STR_PAD_LEFT);  
							$line.="  ".str_pad(number_format($row1['preca'],2),12,' ',STR_PAD_LEFT); 
							$line.="  ".str_pad(number_format($row1['importe'],2),11,' ',STR_PAD_LEFT);    
							$line.="  "."\r\n";
			
			}
		
			
			for ($i = 1; $i < $cont; $i++) {
				$line.="\r\n";
			}
            
			$line.="\t\t\t\t"."SON: ".strtoupper($letra).' '.$row->tmoneda."\r\n";
			$line.=" ".str_pad(number_format($row->totals,2),101,' ', STR_PAD_LEFT)."\r\n";	
			$line.=" ".str_pad(number_format($row->iva,2),101,' ', STR_PAD_LEFT)."\r\n"; 
			$line.="\r\n";
			$line.=" ".str_pad(number_format($row->totalg,2),101,' ', STR_PAD_LEFT)."\r\n"; 
											
		
		}else{
			$line="\r\n";
		}
		
		$nombre='Nota de Credito'.'.txt';
		force_download($nombre,$line);                                                                                                 	
		
	}
	function guiasan($numero){
		
		$mSQL_1  = $this->db->query("SELECT  factura,nguia, fecha, ftraslado,  domiciliop,  regionp,  
		(SELECT nombre from zona WHERE codigo=departap)departap,  
		(SELECT nombre from estado WHERE codigo=provinciap and zona=departap)provinciap,  
		(SELECT nombre from municipio WHERE codigo=distritop and estado=provinciap and zona=departap) distritop,  
		domiciliol,  
		regionl,  
		(SELECT nombre from zona WHERE codigo=departal) departal, 
		(SELECT nombre from estado WHERE codigo=provincial and  zona=departal)provincial,  
		(SELECT nombre from municipio WHERE codigo=distritol and estado=provincial and zona=departal) distritol,  
		 vehiculo, 
		 placa,  cinscripcion,  licencia,  transportista,  direc,  atencion,  tipod,  motivo,  observa, 
		destinatario,  rucd,  tdocu,  ndocu 
		FROM remision as a
		WHERE nguia='$numero'");     
                                                                			                                                                                                                                                     
		$row = $mSQL_1->row(); 
		
		$atencion=$row->atencion;
		
		if(!empty($atencion)){
			
			$atencion=', ATENCION: '.$row->atencion;
		
		}                                                                                                                                       
		
		$mSQL_2 = $this->db->query("SELECT a.codigo,a.cantidad,b.descrip as desca,b.unidad 
		FROM despachos as a
		JOIN sinv as b ON a.codigo=b.codigo
		WHERE a.nguia='$numero' AND a.cantidad>0
		ORDER BY a.codigo");

		$detalle =$mSQL_2->result();
		
		$cant =$mSQL_2->num_rows();
		$cont = (22-$cant);
		
		$line=$error='';
		if ($mSQL_1->num_rows() > 0){
		
			$line.="\r\n";
			$line.="\r\n";	
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";

			$line.="\t\t"." ".str_pad(dbdate_to_human($row->fecha),35,' ');
			$line.=str_pad(dbdate_to_human($row->ftraslado),30,' ')."\r\n";

			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
		
			$line.="\t\t"." ".str_pad($row->domiciliop,53," ");
			$line.=mb_substr(str_pad($row->domiciliol,45,' '),0,38)."\r\n"; 
			
			$line.="\r\n"; 
			
			$line.="\t\t"."  ".str_pad($row->distritop,17," ");
			$line.=str_pad($row->provinciap,15,' ');
			$line.=str_pad($row->departap,19,' ');
			$line.=str_pad($row->distritol,17," ");
			$line.=str_pad($row->provincial,14,' ');
			$line.=str_pad($row->departal,10,' ')."\r\n"; 
			
			$line.="\r\n";
			$line.="\r\n";

			$line.="\t\t".str_pad(' ',75," "); 
			$line.=str_pad($row->vehiculo,20," ")."\r\n"; 

			$line.="\t"." ".str_pad($row->destinatario,78," "); 
			$line.=str_pad($row->placa,20," ")."\r\n"; 

			$line.="\t\t"."  ".str_pad($row->rucd,73," "); 
			$line.=str_pad($row->licencia,20," ")."\r\n";
			
			//$line.="\t\t".str_pad(' ',75," "); 
			//$line.=str_pad($row->licencia,20," ")."\r\n";

			$line.="\r\n";	
			$line.="\r\n";
			$line.="\r\n";
			
			$tot=0;
			foreach($mSQL_2->result_array() as $row1){
			$tot=+$tot+$row1['cantidad'];
			
							$line.="   ".str_pad($row1['codigo'],28,' ');   
							$line.="  ".mb_substr(str_pad($row1['desca']  ,42,' '),0,41);  
							$line.="  ".str_pad($row1['cantidad'],7,' ',STR_PAD_LEFT);
							$line.="  ".str_pad($row1['unidad'],13,' '); 
							$line.="  "."\r\n";
			
			}
		
			
			for ($i = 1; $i < $cont; $i++) {
				$line.="\r\n";
			}
			
			$line.="\t\t\t".'GUIA DE REMISIÒN NÙMERO '.$row->nguia."\r\n";
			$line.="\t\t\t".$row->transportista."\r\n";
			$line.="\t\t\t".$row->direc."\r\n";
			$line.="\t\t\t".$row->tipod.$atencion."\r\n";
			$line.="\t\t\t".$row->observa."\r\n";
			$line.="  ".str_pad($tot,98,' ',STR_PAD_LEFT);
											
		
		}else{
			$line="\r\n";
		}
		
		$nombre='Guìa de Remisiòn'.'.txt';
		force_download($nombre,$line);          
		
	}
	function guia($numero){
		
		$mSQL_1  = $this->db->query("SELECT  factura,nguia, fecha, ftraslado,  domiciliop,  regionp,  
		(SELECT nombre from zona WHERE codigo=departap)departap,  
		(SELECT nombre from estado WHERE codigo=provinciap and zona=departap)provinciap,  
		(SELECT nombre from municipio WHERE codigo=distritop and estado=provinciap and zona=departap) distritop,  
		domiciliol,  
		regionl,  
		(SELECT nombre from zona WHERE codigo=departal) departal, 
		(SELECT nombre from estado WHERE codigo=provincial and  zona=departal)provincial,  
		(SELECT nombre from municipio WHERE codigo=distritol and estado=provincial and zona=departal) distritol,  
		 vehiculo, 
		 placa,  cinscripcion,  licencia,  transportista,  direc,  atencion,  tipod,  motivo,  observa, 
		destinatario,  rucd,  tdocu,  ndocu 
		FROM remision as a
		WHERE nguia='$numero'");     
                                                                			                                                                                                                                                     
		$row = $mSQL_1->row(); 
		
		$atencion=$row->atencion;
		
		if(!empty($atencion)){
			
			$atencion=', ATENCION: '.$row->atencion;
		
		}                                                                                                                                       
		
		$mSQL_2 = $this->db->query("SELECT a.codigo,a.cantidad,b.descrip as desca,b.unidad 
		FROM despachos as a
		JOIN sinv as b ON a.codigo=b.codigo
		WHERE a.nguia='$numero' AND a.cantidad>0
		ORDER BY a.codigo");

		$detalle =$mSQL_2->result();
		
		$cant =$mSQL_2->num_rows();
		$cont = (22-$cant);
		
		$line=$error='';
		if ($mSQL_1->num_rows() > 0){
		
			$line.="\r\n";
			$line.="\r\n";	
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";

			$line.="\t\t"." ".str_pad(dbdate_to_human($row->fecha),35,' ');
			$line.=str_pad(dbdate_to_human($row->ftraslado),30,' ')."\r\n";

			$line.="\r\n";
			$line.="\r\n";
		
			$line.="\t"." ".str_pad($row->domiciliop,52," ");
			$line.=mb_substr(str_pad($row->domiciliol,45,' '),0,38)."\r\n";
						
			//$line.="\t"." ".str_pad($row->distritop,20," ");
			//$line.=str_pad($row->provinciap,15,' ');
			//$line.=str_pad($row->departap,17,' ');
			//$line.=str_pad($row->distritol,20," ");
			//$line.=str_pad($row->provincial,15,' ');
			//$line.=str_pad($row->departal,15,' ')."\r\n";
			
			$line.="\t"." DIST: ".str_pad($row->distritop,46," ");
			$line.="DIST: ".str_pad($row->distritol,39,' ')."\r\n";
			
			$line.="\t"." PROV: ".str_pad($row->provinciap,46," ");
			$line.="PROV: ".str_pad($row->provincial,39,' ')."\r\n";
			
			$line.="\t"." DPTO: ".str_pad($row->departap,46," ");
			$line.="DPTO: ".str_pad($row->departal,39,' ')."\r\n";
			
			$line.="\r\n";
			$line.="\r\n";
			
			
			$line.="\t"." NOMBRE O RAZON SOCIAL: ".str_pad(' ',29," ");
			$line.="VEHICULO/MARCA: ".str_pad($row->vehiculo,20," ")."\r\n"; 
			
			$line.="\t"." ".str_pad($row->destinatario,52," "); 
			$line.="Nº PLACA: ".str_pad($row->placa,26," ")."\r\n";
			
			$line.="\t"." ".str_pad($row->rucd,52," "); 
			$line.="Nº LICENCIA ".str_pad($row->licencia,24," ")."\r\n"; 
			
			$line.="\r\n";	
			$line.="\r\n";	
			$line.="\r\n";
			
			$tot=0;
			foreach($mSQL_2->result_array() as $row1){
			$tot=+$tot+$row1['cantidad'];
			
							$line.="   ".str_pad($row1['codigo'],28,' ');   
							$line.="  ".mb_substr(str_pad($row1['desca']  ,51,' '),0,50);  
							$line.="  ".str_pad($row1['cantidad'],7,' ',STR_PAD_LEFT);
							$line.="  ".str_pad($row1['unidad'],7,' '); 
							$line.="  "."\r\n";
			
			}
		
			
			for ($i = 1; $i < $cont; $i++) {
				$line.="\r\n";
			}
			
			$line.="\t\t\t".'GUIA DE REMISIÒN NÙMERO '.$row->nguia."\r\n";
			$line.="\t\t\t".$row->transportista."\r\n";
			$line.="\t\t\t".$row->direc."\r\n";
			$line.="\t\t\t".$row->tipod.$atencion."\r\n";
			$line.="\t\t\t".$row->observa."\r\n";
			$line.=str_pad(' ',80,' ',STR_PAD_LEFT)."NÚMERO DE BULTOS: ".str_pad($tot,1,' ',STR_PAD_LEFT);
											
		
		}else{
			$line="\r\n";
		}
		
		$nombre='Guìa de Remisiòn'.'.txt';
		force_download($nombre,$line);          
		
	}
	function letra($numero){
		
		
		$f1 = $this->datasis->dameval("SELECT CONCAT('FC ',serief,'-',RIGHT(factura,6)) FROM letras WHERE idpago='$numero' ORDER BY factura ASC  LIMIT 1 ");
		$f2 = $this->datasis->dameval("SELECT CONCAT('FC ',serief,'-',RIGHT(factura,6)) FROM letras WHERE idpago='$numero' ORDER BY factura DESC  LIMIT 1 ");

		IF($f1==$f2){
			$f2='';	
		}
		$mSQL_1  = $this->db->query("SELECT IF(a.moneda='01','S/.','$') AS smoneda,
		IF(a.moneda='01','NUEVOS SOLES','DOLARES AMERICANOS') AS tmoneda, d.descrip as banco, if(a.moneda='01','Dolares Americanos','Nuevos Soles') as moneda  ,a.control, a.cod_cli,c.telefono,c.rifci, CONCAT_WS(' ',dire11,dire12) AS direc,c.nombre,c.repre,c.cirepre,c.fiador,c.domiciliof,c.dnif,c.telefonof,a.tipo,a.fecha,a.vence,a.monto
		FROM itpagos as a
		JOIN scli as c ON a.cod_cli=c.cliente
		JOIN bancos as d ON a.banco=d.codigo
		WHERE  a.id='$numero' AND 
		a.compro='L'"); 
                                                                    			                                                                                                                                                     
		$row = $mSQL_1->row();  

		$monto=$row->monto;
		$letra =numletra($monto);                                                                                                                                   
			
		$line=$error='';
		if ($mSQL_1->num_rows() > 0){
		
			$line.="\r\n";
			$line.="\r\n";	
			$line.="\r\n";
			$line.="\r\n";
			$line.="\r\n";

			$line.="\t\t\t\t\t"."  ".str_pad($row->control,13," ");
			$line.=str_pad($f1,19," ");
			$line.=str_pad('LIMA',14," ")."\r\n";
			 
						
			$line.="\t\t\t\t\t"."  ".str_pad('',13," ");
			$line.=str_pad($f2,19," ");
			$line.=str_pad('',14," ");
			$line.=str_pad(dbdate_to_human($row->fecha),13,' '); 
			$line.=str_pad(dbdate_to_human($row->vence),12,' '); 
			$line.=str_pad($row->smoneda.number_format($row->monto,2),10,' ', STR_PAD_LEFT)."\r\n";
 
			$line.="\r\n";			
			$line.="\r\n";
			$line.="\r\n";
			
   		    	$line.="\t\t\t\t\t\t".str_pad(strtoupper($letra).' '.$row->tmoneda,62," ")."\r\n";
			$line.="\r\n";
			$line.="\r\n";
			
			$line.="\t\t\t\t\t\t".str_pad($row->nombre,62," ")."\r\n";
			
			$line.="\t\t\t\t\t\t\t".str_pad($row->rifci,18," ");
			$line.=" ".str_pad($row->telefono,18," ")."\r\n";
			$line.="  "."\t\t\t\t\t\t".str_pad($row->direc,50," ")."\r\n";
			
			$line.="\r\n";
			
			//$line.="\t\t\t\t\t\t\t\t".str_pad(' ',50," ")."\r\n";
			//$line.="\t\t\t\t\t\t\t\t\t".str_pad($row->fiador,50," ")."\r\n";
			//$line.="\t\t\t\t\t\t\t\t".str_pad($row->domiciliof,50," ")."\r\n";
			//$line.="\r\n";
			//$line.="\r\n";
			//$line.="\t\t\t\t\t\t\t\t\t\t\t".str_pad($row->repre,50," ")."\r\n";
					
		
		}else{
			$line="\r\n";
		}
		
		$nombre='Letra de Cambio'.'.txt';
		force_download($nombre,$line);              
		
	}
}
?>
